<template>
  <div>
    <div class="tools-div">
      <el-button type="success" size="small" @click="handleExportCates">
        导出
      </el-button>
      <el-button type="primary" size="small" @click="handleImportCates">
        导入
      </el-button>
    </div>

    <!---懒加载的树形表格-->
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="loadChildren"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="分类名称" />
      <el-table-column prop="imageUrl" label="图标" #default="scope">
        <img :src="scope.row.imageUrl" width="50" />
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" />
      <el-table-column prop="status" label="状态" #default="scope">
        {{ scope.row.status == 1 ? '正常' : '停用' }}
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
    </el-table>
    <!--导入对话框-->
    <div>
      <el-dialog v-model="dialogImportVisible" title="导入" width="30%">
        <el-form label-width="120px">
          <el-form-item label="分类文件">
            <el-upload
              ref="upload"
              class="upload-demo"
              name="excel"
              action="http://localhost:8501/admin/product/category/import"
              :on-success="onUploadSuccess"
              :headers="headers"
            >
              <el-button type="primary">上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listCatesByPid, ExportCates } from '@/api/category'
import { useApp } from '@/pinia/modules/app'
import { ElMessage } from 'element-plus'
// 定义list属性模型
const list = ref([])
let dialogImportVisible = ref(false)
let headers = ref({
  token: useApp().authorization.token, // 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
})
let upload = ref(null)

const onUploadSuccess = async res => {
  if (+res.code === 200) {
    ElMessage.success('文件上传成功')
    const { code, data } = await listCatesByPid(0)
    if (+code === 200) {
      list.value = data
    }
  } else {
    ElMessage.error(res.message)
  }
  dialogImportVisible.value = false
}
//生命周期方法，加载页面时展示
onMounted(async () => {
  const { code, data } = await listCatesByPid(0)
  if (+code === 200) {
    list.value = data
  }
})
//导入按钮事件函数
const handleImportCates = () => {
  //显示对话框
  dialogImportVisible.value = true
  //清空对话框上传组件缓存
  upload.value.clearFiles()
}
//导出按钮的事件函数
const handleExportCates = () => {
  //异步执行，当响应成功时，then方法会被回调
  ExportCates().then(res => {
    // 创建 Blob 对象，用于包含二进制数据
    const blob = new Blob([res])
    // 创建 a 标签元素，并将 Blob 对象转换成 URL
    const link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    // 设置下载文件的名称
    link.download = '商品分类.xlsx'
    // 模拟点击下载链接
    link.click()
  })
}
// 加载下一级数据的方法
const loadChildren = async (row, treeNode, resolve) => {
  const { code, data } = await listCatesByPid(row.id)
  if (+code === 200) {
    // 返回数据
    resolve(data)
  }
}
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>
